﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WMS.HelperClasses
{
    public static class NumericToArabic
    {

        /////



        /// <summary>

        /// Summary description for NumericToArabic.

        /// </summary>


            private static string GetAla7ad(Int64 Num_Under2)
            {

                string F1;

                switch (Num_Under2)
                {

                    case 1: F1 = "واحد";

                        break;

                    case 2: F1 = "أثنان ";

                        break;

                    case 3: F1 = "ثلاثة";

                        break;

                    case 4: F1 = "أربعة";

                        break;

                    case 5: F1 = "خمسة";

                        break;

                    case 6: F1 = "ستة";

                        break;

                    case 7: F1 = "سبعة";

                        break;

                    case 8: F1 = "ثمانية";

                        break;

                    case 9: F1 = "تسعة";

                        break;

                    default: F1 = "صفر";

                        break;

                }

                return F1;

            }

            private static string GetAl3asharat(int Num2)
            {

                string F2;

                switch (Num2)
                {

                    case 1: F2 = "عشرة";

                        break;

                    case 2: F2 = "عشرون ";

                        break;

                    case 3: F2 = "ثلاثون";

                        break;

                    case 4: F2 = "أربعون";

                        break;

                    case 5: F2 = "خمسون";

                        break;

                    case 6: F2 = "ستون";

                        break;

                    case 7: F2 = "سبعون";

                        break;

                    case 8: F2 = "ثمانون";

                        break;

                    case 9: F2 = "تسعون";

                        break;

                    default: F2 = "صفر";

                        break;

                }



                return F2;



            }

            private static string GetAlmeaat(int Num3)
            {

                string F3;

                switch (Num3)
                {

                    case 1: F3 = "مائة";

                        break;

                    case 2: F3 = "مائتان ";

                        break;

                    case 3: F3 = "ثلاثة مائة";

                        break;

                    case 4: F3 = "أربعة مائة";

                        break;

                    case 5: F3 = "خمسة مائة";

                        break;

                    case 6: F3 = "ستة مائة";

                        break;

                    case 7: F3 = "سبعة مائة";

                        break;

                    case 8: F3 = "ثمان مائة";

                        break;

                    case 9: F3 = "تسعة مائة";

                        break;

                    default: F3 = "صفر";

                        break;

                }

                return F3;

            }

            private static string GetAloloof(int Num4)
            {

                string F4;

                switch (Num4)
                {

                    case 1: F4 = "ألف";

                        break;

                    case 2: F4 = "ألفان";

                        break;

                    case 3: F4 = "ثلاثةألاف";

                        break;

                    case 4: F4 = "أربعةألاف";

                        break;

                    case 5: F4 = "خمسةألاف";

                        break;

                    case 6: F4 = "ستةألاف";

                        break;

                    case 7: F4 = "سبعةألاف";

                        break;

                    case 8: F4 = "ثمانيةألاف";

                        break;

                    case 9: F4 = "تسعةألاف";

                        break;

                    default: F4 = "صفر";

                        break;

                }



                return F4;

            }

            private static string Get3Digits(string Num)
            {

                string ReturnValue;



                string F1, F2, F3;

                int Num_Under3, Num3, Num2, Num_Under2;



                F1 = "";

                F2 = "";

                F3 = "";

                if (Num.ToString() == "")
                {

                    Num = "0";

                }

                int number = int.Parse(Num.ToString());

                if (number >= 0)
                {

                    Num3 = number / 100;

                    Num_Under3 = number % 100;

                    Num2 = (int)Num_Under3 / 10;

                    Num_Under2 = Num_Under3 % 10;



                    // Get Ala7ad

                    F1 = GetAla7ad(Num_Under2);

                    // Get Al3asharat

                    F2 = GetAl3asharat(Num2);

                    if (Num2 != 0)
                    {

                        if ((Num2 == 1) && (Num_Under2 != 0))
                        {

                            F1 = F1 + F2;

                        }

                        else
                        {

                            if ((Num_Under2 != 0) && (Num2 != 0))
                            {

                                F1 = F1 + " و " + F2;

                            }

                            else if (Num2 != 0)
                            {

                                F1 = F2;

                            }

                        }

                    }

                    // Get Almeaat

                    F3 = GetAlmeaat(Num3);

                    if ((Num3 > 0) && (F1 != "صفر"))
                    {

                        F1 = F3 + " و " + F1;

                    }

                    if ((Num3 != 0) && (Num2 == 0) && (Num_Under2 == 0))
                    {

                        F1 = F3;

                    }

                }

                ReturnValue = F1;

                return ReturnValue;

            }

            private static string Get4Digits(string Num)
            {

                string ReturnValue;



                string F1, F2, F3, F4;

                int Num_Under4, Num4, Num_Under3, Num3, Num2, Num_Under2;



                F1 = "";

                F2 = "";

                F3 = "";

                F4 = "";

                if (Num.ToString() == "")
                {

                    Num = "0";

                }

                int number = int.Parse(Num.ToString());

                if (number >= 0)
                {

                    Num4 = number / 1000;

                    Num_Under4 = number % 1000;

                    Num3 = Num_Under4 / 100;

                    Num_Under3 = number % 100;

                    Num2 = (int)Num_Under3 / 10;

                    Num_Under2 = Num_Under3 % 10;



                    // Get Ala7ad

                    F1 = GetAla7ad(Num_Under2);

                    // Get Al3asharat

                    F2 = GetAl3asharat(Num2);

                    if (Num2 != 0)
                    {

                        if ((Num2 == 1) && (Num_Under2 != 0))
                        {

                            F1 = F1 + F2;

                        }

                        else
                        {

                            if ((Num_Under2 != 0) && (Num2 != 0))
                            {

                                F1 = F1 + " و " + F2;

                            }

                            else if (Num2 != 0)
                            {

                                F1 = F2;

                            }

                        }

                    }

                    // Get Almeaat

                    F3 = GetAlmeaat(Num3);

                    if ((Num3 > 0) && (F1 != "صفر"))
                    {

                        F1 = F3 + " و " + F1;

                    }

                    if ((Num3 != 0) && (Num2 == 0) && (Num_Under2 == 0))
                    {

                        F1 = F3;

                    }

                    // Get Aloloof

                    if (Num4 > 0)
                    {

                        F4 = GetAloloof(Num4);

                        if (F1 != "صفر")
                        {

                            F1 = F4 + " و " + F1;

                        }

                        else
                        {

                            F1 = F4;

                        }

                    }

                }

                ReturnValue = F1;

                return ReturnValue;

            }



            private static void Cal3Digits(string strType, ref string strNnumber, ref string ReturnValue)
            {

                string Value;

                if (strNnumber.Length > 0)
                {

                    if (strNnumber.Length < 3)
                    {

                        strNnumber = "000" + strNnumber;

                        strNnumber = strNnumber.Substring(strNnumber.Length - 3, 3);

                    }

                    Value = Get3Digits(strNnumber.Substring(strNnumber.Length - 3, 3));

                    if (Value != "صفر")
                    {

                        Value = Value + strType;

                    }

                    else
                    {

                        Value = "";

                    }

                    if (ReturnValue != "صفر")
                    {

                        if (Value != "")
                        {

                            if (ReturnValue != "")
                            {

                                ReturnValue = Value + " و " + ReturnValue;

                            }

                            else
                            {

                                ReturnValue = Value;

                            }

                        }

                    }

                    else
                    {

                        ReturnValue = Value;

                    }

                    strNnumber = strNnumber.Substring(0, strNnumber.Length - 3);

                }



            }

            private static string Convert(string strNnumber)
            {

                string ReturnValue, strThousand, strMillion, strMilyar, strBillion

                     , strThousandBillion, strMillionBillion, strMilyarBillion, strBillionBillion;

                strThousand = " ألف ";

                strMillion = " مليون ";

                strMilyar = " مليار ";

                strBillion = " بليون ";

                strThousandBillion = " ألف بليون ";

                strMillionBillion = " مليون بليون ";

                strMilyarBillion = " مليار بليون ";

                strBillionBillion = " بليون بليون ";



                ReturnValue = "";



                if (strNnumber.ToString() == "")
                {

                    strNnumber = "0";

                }



                if (strNnumber.Length < 3)
                {

                    strNnumber = "000" + strNnumber;

                    strNnumber = strNnumber.Substring(strNnumber.Length - 3, 3);

                }

                if (strNnumber.Length == 4)
                {

                    ReturnValue = Get4Digits(strNnumber);

                }

                else
                {

                    ReturnValue = Get3Digits(strNnumber.Substring(strNnumber.Length - 3, 3));

                    strNnumber = strNnumber.Substring(0, strNnumber.Length - 3);



                    Cal3Digits(strThousand, ref strNnumber, ref ReturnValue);

                    Cal3Digits(strMillion, ref strNnumber, ref ReturnValue);

                    Cal3Digits(strMilyar, ref strNnumber, ref ReturnValue);

                    Cal3Digits(strBillion, ref strNnumber, ref ReturnValue);

                    Cal3Digits(strThousandBillion, ref strNnumber, ref ReturnValue);

                    Cal3Digits(strMillionBillion, ref strNnumber, ref ReturnValue);

                    Cal3Digits(strMilyarBillion, ref strNnumber, ref ReturnValue);

                    Cal3Digits(strBillionBillion, ref strNnumber, ref ReturnValue);

                }

                return ReturnValue;

            }



            public static string NtoS(string strNnumber)
            {

                double Number;

                Int64 BeforePoint, AfterPoint;

                string ReturnValue, strBeforePoint;

                int intIndex;

                ReturnValue = "";

                try
                {

                    Number = double.Parse(strNnumber);

                    BeforePoint = (Int64)Number;

                    strBeforePoint = BeforePoint.ToString();

                    intIndex = strNnumber.Length - strBeforePoint.Length;

                    if (strNnumber.Length > strBeforePoint.Length)
                    {

                        AfterPoint = Int64.Parse(strNnumber.Substring(strNnumber.Length - intIndex + 1, intIndex - 1));

                    }

                    else
                    {

                        AfterPoint = 0;

                    }

                    if (BeforePoint > 0)
                    {

                        ReturnValue = Convert(BeforePoint.ToString());

                    }

                    if (AfterPoint != 0)
                    {

                        if (ReturnValue != "")
                        {

                            ReturnValue = ReturnValue + " - " + " و " + Convert(AfterPoint.ToString()) + " من " + Convert(((int)(Math.Pow(10, intIndex - 1))).ToString());

                        }

                        else
                        {

                            ReturnValue = Convert(AfterPoint.ToString()) + " من " + Convert(((int)(Math.Pow(10, intIndex - 1))).ToString());

                        }

                    }

                }

                catch (Exception)
                {

                    strNnumber = "0";

                    ReturnValue = Convert(strNnumber);

                }

                return ReturnValue;

            }





            ////
        
    }
}
